package com.gongpingjialoan.activity.car;


import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.SharedPreferencesUtil;
import com.gongpingjialoan.widget.DateTimePickDailog;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SeeCarFragment extends DialogFragment implements OnClickListener, NetDataJson.OnNetDataJsonListener {

    private SecondCarFragment secondCarFragment;

    private View view;

    private TextView toseeTextView;

    private ImageView closeImageView;

    private String car_id;

    private Button submitButton;

    private EditText phoneEditText;

    private NetDataJson netWork;

    private LoadingDialog loadingDialog = null;

    private EditText nameEditText;

    private DateTimePickDailog datePickerDialog;

    private LinearLayout main;

    public SeeCarFragment(SecondCarFragment secondCarFragment) {
        this.secondCarFragment = secondCarFragment;
    }

    @SuppressLint("InflateParams")
    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.dialogs);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        loadingDialog = new LoadingDialog(getActivity(), "加载中...");
        datePickerDialog = new DateTimePickDailog(getActivity(), 7, this);

        view = inflater.inflate(R.layout.confirm_see_car, null, false);

        main = (LinearLayout) view.findViewById(R.id.main);
        toseeTextView = (TextView) view.findViewById(R.id.time_to_see_view);
        closeImageView = (ImageView) view.findViewById(R.id.close);
        submitButton = (Button) view.findViewById(R.id.confirm);
        phoneEditText = (EditText) view.findViewById(R.id.phone_view);
        nameEditText = (EditText) view.findViewById(R.id.name);
        toseeTextView.setOnClickListener(this);
        closeImageView.setOnClickListener(this);
        submitButton.setOnClickListener(this);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);
        String name = (String) SharedPreferencesUtil.get(getActivity(), "discount_name", "");
        if (!TextUtils.isEmpty(name)) {
            nameEditText.setText(name);
        }
        if (GPJApplication.getInstance().isLogin()) {
            UserManager mUserManager = new UserManager(getActivity());
            mUserManager = mUserManager.LoadUserInfo();
            phoneEditText.setText(mUserManager.getPhone());
        }
        return dialog;
    }


    public void setCar_id(String car_id) {
        this.car_id = car_id;
    }


    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        final DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        final WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = dm.widthPixels;
        layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.confirm_see_car);
        layoutParams.gravity = Gravity.BOTTOM;
        getDialog().getWindow().setAttributes(layoutParams);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    }


    @Override
    public void onClick(View arg0) {
        if (arg0 == toseeTextView) {
            datePickerDialog.show();
        } else if (arg0 == closeImageView) {
            dismiss();
        } else if (arg0 == submitButton) {
            submit();
        }
    }

    private String data = "";
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    public void setTime(String str, Date date, String time) {
        String newTime = time.replace("点", "");
        data = df.format(date) + " " + newTime + ":00";
        // Toast.makeText(getActivity(), data, Toast.LENGTH_SHORT).show();
        toseeTextView.setText(str);
    }

    private void submit() {
        if (TextUtils.isEmpty(nameEditText.getText().toString())) {
            Toast.makeText(getActivity(), "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phoneEditText.getText().toString().length() < 11) {
            Toast.makeText(getActivity(), "请输入11位手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

//        if (!Utils.isValidMobilePhoneNumber(phoneEditText.getText().toString())) {
//            Toast.makeText(getActivity(), "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
//            return;
//        }

        if (TextUtils.isEmpty(data)) {
            Toast.makeText(getActivity(), "请选择正确的时间", Toast.LENGTH_SHORT).show();
            return;
        }

        if (null != netWork) {
            netWork.cancelTask();
        }
        SharedPreferencesUtil.put(getActivity(), "discount_name", nameEditText.getText().toString());
        loadingDialog.show();
        netWork = new NetDataJson(this);
        netWork.setUrl(API.appointdiscount);
        netWork.addParam("phone", phoneEditText.getText().toString());
        netWork.addParam("contact", nameEditText.getText().toString());
        netWork.addParam("promo_car_id", car_id);
        netWork.addParam("date_time", data);
        netWork.addParam("device_code", GPJApplication.getInstance().getDeviceCode());
        netWork.request("post");
    }


    @Override
    public void onDataJsonError(String errorMessage) {
        loadingDialog.dismiss();
        Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onDataJsonUpdate(JSONObject json) {
        loadingDialog.dismiss();
        Toast.makeText(getActivity(), "预约成功！", Toast.LENGTH_SHORT).show();
        if (secondCarFragment != null)
            secondCarFragment.appiontSuccess();
        dismiss();
        if (MainActivity.main != null)
            MainActivity.main.mRecomendDiscountFragment.updateDiscountListItem();
    }

}
